package cn.wit.summit.chwan.ui.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.adapters.GameLibraryTwoAdapter;
import cn.wit.summit.chwan.models.event.NotNetEvent;
import cn.wit.summit.chwan.models.request.GamePlayTypeRequest;
import cn.wit.summit.chwan.models.response.GamePlayTypeResponse;
import cn.wit.summit.chwan.models.response.ResponseBase;
import cn.wit.summit.chwan.retrofit.ApiUtil;
import cn.wit.summit.chwan.retrofit.MyCallback;
import cn.wit.summit.chwan.retrofit.RetrofitManager;
import cn.wit.summit.chwan.ui.base.BaseActivity;
import cn.wit.summit.chwan.utils.AccountBean;
import cn.wit.summit.chwan.utils.AccountUtil;
import cn.wit.summit.chwan.widgets.refreshLayout.SmartRefreshLayout;
import cn.wit.summit.chwan.widgets.refreshLayout.api.RefreshLayout;
import cn.wit.summit.chwan.widgets.refreshLayout.listener.OnLoadMoreListener;
import cn.wit.summit.chwan.widgets.refreshLayout.listener.OnRefreshListener;
import retrofit2.Retrofit;

/**
 * Created by Alan on 2018/9/28.
 */

public class GamePlayTypeActivty extends BaseActivity {

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private RetrofitManager retrofitManager;
    private Retrofit retrofit;
    private ApiUtil api;

    private GameLibraryTwoAdapter adapter;
    private List<GameLibraryTwoAdapter.ViewBean> viewBeans = new ArrayList<>();
    private int pn = 1;
    private LinearLayoutManager layoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_game_library_two);
        ButterKnife.bind(this);

        setBarTitleTx(getResources().getString(R.string.gamelib_t5_str));
        retrofitManager = RetrofitManager.getInstance(mContext);
        retrofit = retrofitManager.getRetrofit();
        api = retrofit.create(ApiUtil.class);

        layoutManager = new LinearLayoutManager(mAty);
        adapter = new GameLibraryTwoAdapter(mAty, viewBeans);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        recyclerView.setFocusable(false);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getIndexDataForFirst();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                getIndexData();
            }
        });

        getIndexDataForFirst();
    }

    @Override
    public void reGetData() {
        super.reGetData();
        getIndexDataForFirst();
    }

    private void getIndexDataForFirst() {
        refreshLayout.setEnableLoadMore(true);
        pn = 1;
        getIndexData();
    }

    private void getIndexData() {
        int uId = 0;
        AccountBean accountBean = AccountUtil.getInstance().getAccountData();
        if (accountBean != null) {
            uId = AccountUtil.getInstance().getUid();
        }

        GamePlayTypeRequest request = retrofitManager.gamePlayTypeRequest(uId, pn);
        api.gamePlayTypeV3(request).enqueue(new MyCallback<ResponseBase<GamePlayTypeResponse>>() {
            @Override
            public void onSuccess(ResponseBase<GamePlayTypeResponse> model) {
                GamePlayTypeResponse data = model.getData_info();
                updataUi(data);
            }

            @Override
            public void onFailure(int code, String msg) {
                showToast(msg);
                refreshLayout.finishLoadMore();
            }
        });
    }

    private int insetPo = 2;
    private void updataUi(GamePlayTypeResponse data) {
        if (data == null) {
            return;
        }
        int po = 0;
        if (pn == 1) {
            insetPo = 2;
            viewBeans.clear();
            viewBeans.add(new GameLibraryTwoAdapter.ViewBean(GameLibraryTwoAdapter.ViewType.TYPE_LINE));
            viewBeans.add(new GameLibraryTwoAdapter.ViewBean(GameLibraryTwoAdapter.ViewType.TYPE_LINE));
        } else {
            po = viewBeans.size();
        }

        List<GamePlayTypeResponse.ListBean> tempList = data.getList();
        List<GamePlayTypeResponse.ListBean> temp = new ArrayList<>();
        for (int i = 0, itemPo = 0; tempList != null && i < tempList.size(); i++) {
            temp.add(tempList.get(i));
            if (temp.size()>=3) {
                List<GamePlayTypeResponse.ListBean> list = new ArrayList<>();
                list.addAll(temp);
                GameLibraryTwoAdapter.ViewBean viewBean = new GameLibraryTwoAdapter.ViewBean(GameLibraryTwoAdapter.ViewType.TYPE_ITEM_V2, list);
                viewBeans.add(insetPo+itemPo, viewBean);
                itemPo++;
                temp.clear();
            }
        }

        insetPo = viewBeans.size();
        if (pn == 1) {
            viewBeans.add(new GameLibraryTwoAdapter.ViewBean(GameLibraryTwoAdapter.ViewType.TYPE_LINE));
        }
        if (pn == 1) {
            GameLibraryTwoAdapter.ViewBean viewBean = new GameLibraryTwoAdapter.ViewBean(GameLibraryTwoAdapter.ViewType.TYPE_BOTTOM, data.getBanner());
            viewBeans.add(viewBean);
        }

        adapter.notifyItemRangeChanged(po, viewBeans.size());
        if (refreshLayout != null) {
            if (pn != 1 && (data == null || data.getList() == null
                    || data.getList().size() == 0)) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                if (pn == 1)
                    refreshLayout.finishRefresh();
                else
                    refreshLayout.finishLoadMore();
                pn += 1;
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(NotNetEvent notNetEvent) {
        if (viewBeans != null && viewBeans.size() > 0
                && !notNetEvent.isHasNet) return;
        super.onEventMainThread(notNetEvent);
    }
}
